package org.acmclass.fatworm.test.plaintext;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import org.acmclass.fatworm.test.FatTestException;

public class FatFileWriter {
	private PrintWriter out = null;

	public FatFileWriter(String filename) throws FatTestException {
		this(filename, false);
	}

	public FatFileWriter(String filename, boolean autoFlush)
			throws FatTestException {
		try {
			File file = new File(filename);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			out = new PrintWriter(filename);
			if (autoFlush) {
				out = new PrintWriter(out, true);
			}
		} catch (IOException e) {
			throw new FatTestException("can't write file " + filename, e);
		}
	}

	public void write(String str) {
		out.print(str);
	}

	public void writeLine(String line) {
		out.println(line);
	}

	public void writeTag(Tag tag) {
		out.println(tag.toString());
	}

	public void writeComment(String comments) {
		for (String comment : comments.split("\n")) {
			writeLine("#" + comment);
		}
	}

	public void writeEnd() {
		out.println(";");
	}

	public void close() {
		if (out != null)
			out.close();
	}
}
